import BScroll from 'better-scroll';
import { mapState, mapMutations } from 'vuex'
import {
	getDATE,
} from '../../config/util'


const festival = {
	"2017-08-01":"建军节",	
	"2017-08-28":"七夕节",	
	"2017-09-10":"教师节",
	"2017-10-01":"国庆节",
	"2017-10-04":"中秋节",
	"2017-10-28":"重阳节",
	"2017-10-28":"重阳节",
	"2017-12-25":"圣诞节",
	"2018-01-01":"元旦",
	"2018-01-24":"腊八节",
	"2018-02-14":"情人节",
	"2018-02-15":"除夕",
	"2018-02-16":"春节",
	"2018-03-02":"元宵节",
	"2018-03-08":"妇女节",
	"2018-03-12":"植树节",
	"2018-04-05":"清明节",
	"2018-05-01":"劳动节",
	"2018-05-04":"青年节",
	"2018-05-13":"母亲节",
	"2018-06-01":"儿童节",
	"2018-07-01":"建党节",	
	"2018-08-01":"建军节",	
};
const price = {
	"2017-08-07":"991",	
	"2017-08-08":"992",	
	"2017-08-09":"993",
	"2017-08-10":"994",
	"2017-08-11":"995",
	"2017-08-12":"996",
	"2017-08-13":"997",
	"2017-08-14":"998",
	"2017-08-15":"999",
	"2017-08-16":"990",
	"2017-08-17":"981",
	"2017-08-18":"971",
	"2017-08-19":"961",
	"2017-08-20":"951",
	"2017-08-21":"941",
	"2017-08-22":"931",
	"2017-08-23":"921",
	"2017-08-24":"911",
	"2017-08-25":"901",
	"2017-08-26":"191",
	"2017-08-27":"291",
	"2017-08-28":"391",	
	"2017-08-29":"491",	
};

export default {
	name: 'date',
	data() {
		return {
			activeTab: "depDate",
			scrollY:0,
			fromIn:this.$route.query.from ? false : true,
			timer :0,//解决ios兼容问题
			checked:{
				dep:{
					date:"",
					price:null
				},
				arr:{
					date:"",
					price:null
				}
			}
		}
	},
	computed: {		
		...mapState([
			'Date','isBack',
		]),
		
		Date_() {
			let myDate = new Date();
			let nowYear = myDate.getFullYear();
			let nowMonth = myDate.getMonth() + 1;
			let dateArr = [];
			for(let i = -1; i < 12; i++) {
				let nextMonthCount = Number(nowMonth) + i;
				let nextYear = Number(nowYear) + parseInt(nextMonthCount / 12);
				let nextMonth = (nextMonthCount % 12) + 1;
				let dayCount = new Date(nextYear, nextMonth, 0).getDate();//每月的天数		
			  	let weeks = [];
			  	let week = [];
				for(let j = 1; j <= dayCount; j++) {
					let str = nextYear+"/"+nextMonth+"/"+1;	
					let arr = str.split(/[- : \/]/);
					let wk = new Date(arr[0], arr[1]-1, arr[2]).getDay();//周几	//兼容ios
					//判断1号从什么位置开始
					if(j==1){							
						for(let m=0;m<wk;m++){
							week.push("");
						}
					}
					let postVal = nextYear+"-"+(nextMonth<10?"0"+nextMonth:nextMonth)+"-"+(j<10?"0"+j:j);
					week.push({
						fullVal:nextYear+"年"+nextMonth+"月"+j+"日",
						postVal:postVal,
						mdVal:(nextMonth<10?"0"+nextMonth:nextMonth)+"月"+(j<10?"0"+j:j)+"日",
						simpleVal:this.matchFestival(postVal,j),
						price:this.matchPrice(postVal),
						checked:false,
					});
					if(week.length==7||j==dayCount){
						weeks.push(week);
						week = [];
					}
				}
				let months = {
					title: nextYear + "年" + nextMonth + "月",
					weeks: weeks,					
				};
				dateArr.push(months);				
			}
			return dateArr;
		},
		
	},
	methods: {
		...mapMutations([
			'RECORD_DATE',
		]),
		handleTabChange(val) {
			this.activeTab = val;
		},
		_initScroll() {
			var that = this;
			this.date_scroll = new BScroll(this.$refs.date_scroll, {
				probeType: 3,
				click: true
			})
			this.date_scroll.on('scroll', function(pos) {
				that.scrollY = Math.abs(Math.round(pos.y));
			})
		},
		//匹配价格日历
		matchPrice(postVal){			
			if(price[postVal]) return "￥"+price[postVal];
			else return "";
		},
		//日期匹配节日
		matchFestival(postVal,j){
			var today = getDATE(0,"YYYY-MM-DD");
			for(let i in festival){
				if(i==postVal){
					if(today>postVal){
						return "<span class='hanzi' style='color:#999;'>"+festival[postVal]+"</span>";
						break;
					}else{
						return "<span class='hanzi'>"+festival[postVal]+"</span>";
						break;
					}
				}
			}			
			if(postVal==today) return "<span class='hanzi' style='color:red;'>今天</span>";
			else if(today>postVal){
				return "<span style='color:#999;'>"+j+"</span>";
			}else{
				return "<span>"+j+"</span>";
			}
		},
		check(val){
			/*解决iscroll在ios中重复执行点击事件*/
			if(this.timer==0){
				this.timer = new Date().getTime();
			}else{
				var time = new Date().getTime();
				if(time - this.timer < 500){
		            this.timer = time;
		            return;
		        }else{
		            this.timer = time;
		        }
			}
			/*结束*/
			var today = getDATE(0,"YYYY-MM-DD");
			if(today>val.postVal){
				return false;
			}else{				
				this.isBackHandle(val);
			}
		},
		//来自首页的处理
		fromIndex(val){
			var that = this;			
			if(this.isBack){
				if(this.Date.type=="dep"){
					this.activeTab = "arrDate";
					this.RECORD_DATE(val);
					this.RECORD_DATE("arr");	
					this.checked.dep = {date:val.mdVal,price:val.price};
					val.checked = true;
					return;
				}
				if(this.Date.type=="arr"){
					if(val.postVal<this.Date.postDepDate) return false;
					this.RECORD_DATE(val);
					this.checked.arr = {date:val.mdVal,price:val.price};
					val.checked = true;
					setTimeout(function(){
						that.$router.push({ path: '/' });
					},300)
				}				
			}else{
				this.RECORD_DATE(val);
				this.$router.push({ path: '/' });
			}
		},
		//来自shopping页的处理
		formShopping(val){
			this.RECORD_DATE(val);
			if(this.Date.type == 'dep'){
				this.$router.push({ path: '/flight' });
			}
			if(this.Date.type == 'arr'){				
				this.$router.push({ path: '/flightBack' });
			}
		},
		isBackHandle(val){
			if(this.$route.query.from){	
				this.formShopping(val);
			}else{	
				this.fromIndex(val);
			}
		},
	},
	created() {
		document.title = "日期选择";
		if(this.Date.type=="arr") this.activeTab = "arrDate";
		this.$nextTick(() => {
			this._initScroll();
		});
	},
	
}